subroutine bagi_dua(fungsi, eps_mesin, eps, a, b, c)
	implicit none
	double precision fungsi, eps_mesin, eps, a, b, c, temp_fa, temp_fb, temp_fc,lebar,a_lama,b_lama
	character(LEN=13) selang
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	print "(40(' '),A)", "Metode Bagi Dua"
	if ((fungsi(a) * fungsi(b)).LT.0) then
		!print
		print "(90('-'))"
print "(2X,A,7X,A,9X,A,9X,A,7X,A,6X,A,6X,A,5X,A,6X,A)", "r", "a", "c", "b", "f(a)", "f(c)", "f(b)", "Selang", "Lebarnya"
		print "(90('-'))"
		do 
			c = (a+b)/2
			temp_fa = fungsi(a)
			temp_fb = fungsi(b)
			temp_fc = fungsi(c)
			a_lama = a
			b_lama = b
			if (abs(temp_fc).LT.eps_mesin) then
				a = c
				b = c
			else
				if ((temp_fa * temp_fc).LT.0) then
					b = c	! selang baru [a,c]
					selang = "   [a, c]"
				else
					a = c   ! selang baru [c,b]
					selang = "   [c, b]"				
				end if
			end if
			lebar = abs(a-b)
			
			!print
print"(I4,F10.6,F10.6,F10.6,F10.6,F10.6,F10.6,A,F10.6)", counter, a_lama, c, b_lama, temp_fa, temp_fc, temp_fb, selang, lebar
			
			counter = counter + 1
			if (lebar.LT.eps) then
				exit
			end if
			if (counter.GE.batas) then
				print*,"Batas iterasi maksimum : 1000"
				exit
			end if
		end do
		print "(90('-'))"
	else
		print*,"Nilai fungsi di a, f(a) = ", fungsi(a), " dan b, f(b) = ",fungsi(b)," keduanya bertanda sama."
		print*,"Sehingga tidak ada solusi"
	end if
end

subroutine bagi_dua_galat(fungsi, eps_mesin, galat, a, b, c)
	implicit none
	double precision fungsi, eps_mesin, galat, a, b, c, temp_fa, temp_fb, temp_fc,temp_galat,c_lama,a_lama,b_lama
	character(LEN=13) selang
	integer*2 counter, batas
	parameter(batas=1000)
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(I4,F10.6,F10.6,F10.6,F10.6,F10.6,F10.6,A,F10.6)
102 format(90('-'))
103 format(2X,A,7X,A,9X,A,9X,A,7X,A,6X,A,6X,A,5X,A,6X,A)
104 format(I4,F10.6,F10.6,F10.6,F10.6,F10.6,F10.6,A,F10.6)
	
	counter = 0
	c_lama = 0
	!print
	
	print "(40(' '),A)", "Metode Bagi Dua"
	write(12,"(40(' ')A)") "Metode Bagi Dua"
	if ((fungsi(a) * fungsi(b)).LT.0) then
		print "(90('-'))"
		write(12,102) 
print "(2X,A,7X,A,9X,A,9X,A,7X,A,6X,A,6X,A,5X,A,6X,A)", "r", "a", "c", "b", "f(a)", "f(c)", "f(b)", "Selang", "Lebarnya"
		write(12,103) "r", "a", "c", "b", "f(a)", "f(c)", "f(b)", "Selang", "Lebarnya"
		print "(90('-'))"
		write(12,102)
		print "(90('-'))"
		write(12,102)
		
		do 
			c = (a+b)/2
			temp_fa = fungsi(a)
			temp_fb = fungsi(b)
			temp_fc = fungsi(c)
			a_lama = a
			b_lama = b
			if (abs(temp_fc).LT.eps_mesin) then
				a = c
				b = c
			else
				if ((temp_fa * temp_fc).LT.0) then
					b = c	! selang baru [a,c]
					selang = "   [a, c]"
				else
					a = c   ! selang baru [c,b]
					selang = "   [c, b]"				
				end if
			end if
			temp_galat = abs((c-c_lama)/c)
			c_lama = c
			!print
print "(I4,F10.6,F10.6,F10.6,F10.6,F10.6,F10.6,A,F10.6)", counter, a_lama, c, b_lama, temp_fa, temp_fc, temp_fb,selang,temp_galat
write(12,104) counter, a_lama, c, b_lama, temp_fa, temp_fc, temp_fb,selang,temp_galat
			counter = counter + 1
			if (temp_galat.LT.galat) then
				exit
			end if
			if (counter.GE.batas) then
				print*,"Batas iterasi maksimum : 1000"
				exit
			end if
		end do
		print "(90('-'))"
		write (12, 102)
	else
print*,"Nilai fungsi di a, f(a) = ", fungsi(a), " dan b, f(b) = ",fungsi(b)," keduanya bertanda sama."
print*,"Sehingga tidak ada solusi"
write(12,*) "Nilai fungsi di a, f(a) = ", fungsi(a), " dan b, f(b) = ",fungsi(b)," keduanya bertanda sama."
write(12,*) "Sehingga tidak ada solusi"	
	end if
	close(12)
end

subroutine regula_falsi(fungsi, eps_mesin, delta, eps, a, b, c)
	implicit none
	DOUBLE PRECISION fungsi, a, b, c, eps, eps_mesin, delta
	DOUBLE PRECISION f_a, f_b
	integer*2 counter, mandek_kiri, mandek_kanan, batas
	parameter(batas=1000)
	!var untuk nge print
	DOUBLE PRECISION temp_a, temp_b, temp_c, temp_f_a, temp_f_b
	character(LEN=8) selang
	
	counter = 0
	f_a = fungsi(a)
	f_b = fungsi(b)
	print "(40(' '),A)", "Metode Regula Falsi"
	if ((f_a * f_b).LT.0) then
		mandek_kanan = 1
		mandek_kiri = 1
		print "(90('-'))"
		print "(3X,A,6X,A,9X,A,9X,A,8X,A,6X,A,6X,A,4X,A,2X,A)","r","a","c","b","f(a)","f(c)","f(b)","Selang Baru","Lebarnya"
		print "(90('-'))"
		do
			c = b - ((f_b*(b-a))/(f_b-f_a))
			temp_a = a
			temp_b = b
			temp_c = c
			temp_f_a = f_a
			temp_f_b = f_b
			if (abs(fungsi(c)).LT.eps_mesin) then
				!f(c) = 0, c adalah akar
				a = c
				b = c
			else
				if ((fungsi(a)*fungsi(c)).LT.0) then
					b = c
					f_b = fungsi(c)
					mandek_kiri = mandek_kiri + 1
					mandek_kanan = 0
					selang = "[a,c]"
					if (mandek_kiri.GT.1) then
						f_a = f_a / 2
					end if
				else
					a = c
					f_a = fungsi(c)
					mandek_kanan = mandek_kanan + 1
					mandek_kiri = 0
					selang = "[c,b]"
					if (mandek_kanan.GT.1) then
						f_b = f_b / 2
					end if
				end if
			end if
			
			!print
			print"(I4,6F10.6,6X,A,F10.6)",counter,temp_a,temp_c,temp_b,temp_f_a,fungsi(temp_c),temp_f_b,selang,abs(a-b)
					
			counter = counter + 1
			if ((abs(a-b).LT.eps).OR.((abs(fungsi(c))).LT.delta)) then
				exit
			end if
			if (counter.GE.batas) then
				print*,"Batas iterasi maksimum : 1000"
				exit
			end if
		end do
		print "(90('-'))"
	else
print*,"Nilai fungsi di a, f(a) = ", f_a, " dan b, f(b) = ",f_b," keduanya bertanda sama."
print*,"Sehingga tidak ada solusi"
	end if
end

subroutine regula_falsi_galat(fungsi, eps_mesin, delta, galat, a, b, c)
	implicit none
	DOUBLE PRECISION fungsi, a, b, c, galat, eps_mesin, delta
	DOUBLE PRECISION f_a, f_b, temp_galat, c_lama
	integer*2 counter, mandek_kiri, mandek_kanan, batas
	parameter(batas=1000)
	!var untuk nge print
	DOUBLE PRECISION temp_a, temp_b, temp_c, temp_f_a, temp_f_b
	character(LEN=8) selang
	c_lama = 0
	counter = 0
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(40(' '),A)
102 format(90('-'))
103 format(3X,A,6X,A,9X,A,9X,A,8X,A,6X,A,6X,A,4X,A,2X,A)
104 format(I4,6F10.6,6X,A,F10.6)

	f_a = fungsi(a)
	f_b = fungsi(b)
	print "(40(' '),A)", "Metode Regula Falsi"
	write(12,101) "Metode Regula Falsi"
	if ((f_a * f_b).LT.0) then
		mandek_kanan = 1
		mandek_kiri = 1
		print "(90('-'))"
		write(12,102)
		print "(3X,A,6X,A,9X,A,9X,A,8X,A,6X,A,6X,A,4X,A,2X,A)","r","a","c","b","f(a)","f(c)","f(b)","Selang Baru","Galat"
		write(12,103) "r","a","c","b","f(a)","f(c)","f(b)","Selang Baru","Galat"
		print "(90('-'))"
		write(12,102)
		print "(90('-'))"
		write(12,102)
		
		do
			c = b - ((f_b*(b-a))/(f_b-f_a))
			temp_a = a
			temp_b = b
			temp_c = c
			temp_f_a = f_a
			temp_f_b = f_b
			if (abs(fungsi(c)).LT.eps_mesin) then
				!f(c) = 0, c adalah akar
				a = c
				b = c
			else
				if ((fungsi(a)*fungsi(c)).LT.0) then
					b = c
					f_b = fungsi(c)
					mandek_kiri = mandek_kiri + 1
					mandek_kanan = 0
					selang = "[a,c]"
					if (mandek_kiri.GT.1) then
						f_a = f_a / 2
					end if
				else
					a = c
					f_a = fungsi(c)
					mandek_kanan = mandek_kanan + 1
					mandek_kiri = 0
					selang = "[c,b]"
					if (mandek_kanan.GT.1) then
						f_b = f_b / 2
					end if
				end if
			end if
			temp_galat = abs((c-c_lama)/c)
			c_lama = c
			!print
print"(I4,6F10.6,6X,A,F10.6)",counter,temp_a,temp_c,temp_b,temp_f_a,fungsi(temp_c),temp_f_b,selang,temp_galat
write(12,104) counter,temp_a,temp_c,temp_b,temp_f_a,fungsi(temp_c),temp_f_b,selang,temp_galat
					
			counter = counter + 1
			if ((temp_galat.LT.galat).OR.((abs(fungsi(c))).LT.delta)) then
				exit
			end if
			if (counter.GE.batas) then
				print*,"Batas iterasi maksimum : 1000"
				exit
			end if
		end do
		print "(90('-'))"
		write(12,103)
	else
print*,"Nilai fungsi di a, f(a) = ", f_a, " dan b, f(b) = ",f_b," keduanya bertanda sama."
print*,"Sehingga tidak ada solusi"
write(12,*) "Nilai fungsi di a, f(a) = ", f_a, " dan b, f(b) = ",f_b," keduanya bertanda sama."
write(12,*) "Sehingga tidak ada solusi"
	end if
	close(12)
end

subroutine secant(fungsi, eps_mesin, eps, x0, x1, x)
!! x0, x1 tebakan awal akar
!! out ke x
	implicit none
	DOUBLE PRECISION fungsi, x0, x1, x, x_sebelumnya, y0, y1, eps, eps_mesin
	logical berhenti
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	berhenti = .FALSE.
	print "(40(' '),A)", "Metode Secant"
	print "(35('-'))"
	print "(3X,A,4X,A,3X,A)", "i","x(r)", "|x(r+1)-x(r)| "
	print "(35('-'))"
	do
		x_sebelumnya = x1
		y0 = fungsi(x0)
		y1 = fungsi(x1)
		if (abs(y1-y0).LT.eps_mesin) then
			berhenti = .TRUE.
			x = 9999
		else
			x = x - (y1*(x1-x0)/(y1-y0))
			x0 = x1
			x1 = x
		end if	
		print "(I4,F10.6,F10.6)", counter, x, abs(x-x_sebelumnya)
		counter = counter + 1
		if ((abs(x-x_sebelumnya).LT.eps).OR.berhenti) then
			exit
		end if
		if (counter.GE.batas) then
			print*,"Batas iterasi maksimum : 1000"
			exit
		end if
	end do
	print "(35('-'))"
end

subroutine secant_galat(fungsi, eps_mesin, galat, x0, x1, x)
!! x0, x1 tebakan awal akar
!! out ke x
	implicit none
	DOUBLE PRECISION fungsi, x0, x1, x, x_sebelumnya, y0, y1, galat, eps_mesin
	logical berhenti
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	berhenti = .FALSE.
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(40(' '),A)
102 format(35('-'))
103 format(3X,A,4X,A,3X,A)
104 format(I4,F10.6,F10.6)
		
	print "(40(' '),A)", "Metode Secant"
	write(12,101) "Metode Secant"
	print "(35('-'))"
	write(12,102)
	print "(3X,A,4X,A,3X,A)", "i","x(r)", "|x(r+1)-x(r)| "
	write(12,103) "i","x(r)", "|x(r+1)-x(r)| "
	print "(35('-'))"
	write(12,102)
	print "(35('-'))"
	write(12,102)
	do
		x_sebelumnya = x1
		y0 = fungsi(x0)
		y1 = fungsi(x1)
		if (abs(y1-y0).LT.eps_mesin) then
			berhenti = .TRUE.
			x = 9999
		else
			x = x - (y1*(x1-x0)/(y1-y0))
			x0 = x1
			x1 = x
		end if	
		print "(I4,F10.6,F10.6)", counter, x, abs((x-x_sebelumnya)/x)
		write(12,104) counter, x, abs((x-x_sebelumnya)/x)
		counter = counter + 1
		if ((abs((x-x_sebelumnya)/x).LT.galat).OR.berhenti) then
			exit
		end if
		if (counter.GE.batas) then
			print*,"Batas iterasi maksimum : 1000"
			exit
		end if
	end do
	print "(35('-'))"
	write(12,102)
	close(12)
end


subroutine newton_raphson(fungsi, fungsi_aksen, fungsi_double_aksen, x, eps)
	implicit none
	double precision fungsi, fungsi_aksen, fungsi_double_aksen, x, eps, x_lama, selisih, konvergensi
	logical ga_ada_solusi
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	ga_ada_solusi = .false.
	print "(40(' '),A)", "Metode Newton Raphson"
	if (fungsi_aksen(x).eq.0.0) then
		print*, "Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
	else
		konvergensi = abs((fungsi(x)*fungsi_double_aksen(x))/(fungsi_aksen(x)**2))
		if (konvergensi.lt.1.0) then
			print "(35('-'))"
			print "(3X,A,4X,A,3X,A)", "i", "x(r)", "|x(r+1)-x(r)|"
			print "(35('-'))"	
			do
				x_lama = x
				if ((fungsi_aksen(x).eq.0.0)) then
					print "(35('-'))"
					print*, "Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
					ga_ada_solusi = .true.
					exit
				end if
				x = x - (fungsi(x)/fungsi_aksen(x))
				selisih = abs(x-x_lama)
				if (counter.eq.0) then
					print "(I4,F10.6,F10.6)", counter, x_lama, 0.0
				else
					print "(I4,F10.6,F10.6)", counter, x_lama, selisih
				end if
				counter = counter + 1
				if (selisih.LT.eps) then
					exit
				end if
				if (counter.GE.batas) then
					print*,"Batas iterasi maksimum : 1000"
					exit
				end if
			end do
			if (ga_ada_solusi) then
				x = 0
			else
				print "(35('-'))"
			end if
		else
			print*, "Fungsi tidak konvergen"
		end if
	end if
		
END

subroutine newton_raphson_galat(fungsi, fungsi_aksen, fungsi_double_aksen, x, galat)
	implicit none
	double precision fungsi, fungsi_aksen, fungsi_double_aksen, x, galat, x_lama, temp_galat, konvergensi
	logical ga_ada_solusi
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(40(' '),A)
102 format(35('-'))
103 format(3X,A,4X,A,3X,A)
104 format(I4,F10.6,F10.6)
	
	ga_ada_solusi = .false.
	print "(40(' '),A)", "Metode Newton Raphson"
	write(12,101) "Metode Newton Raphson"
	
	if (fungsi_aksen(x).eq.0) then
		print*, "Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
		write(12,*)"Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
	else
		konvergensi = abs((fungsi(x)*fungsi_double_aksen(x))/(fungsi_aksen(x)**2))
		if (konvergensi.lt.1) then
			print "(35('-'))"
			write(12,102)
			print "(3X,A,4X,A,3X,A)", "i", "x(r)", "|x(r+1)-x(r)|"
			write(12,103) "i", "x(r)", "|x(r+1)-x(r)|"
			print "(35('-'))"
			write(12,102)
			do
				x_lama = x
				if ((fungsi_aksen(x).eq.0)) then
					print "(35('-'))"
					write(12,102)
					print*, "Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
					write(12,*) "Turunan Pertama fungsi dengan nilai ", x, " bernilai 0"
					ga_ada_solusi = .true.
					exit
				end if
				x = x - (fungsi(x)/fungsi_aksen(x))
				temp_galat = abs((x-x_lama)/x)
				if (counter.eq.0) then
					print "(I4,F10.6,F10.6)", counter, x_lama, 0.0
					write(12,104) counter, x_lama, 0.0
				else
					print "(I4,F10.6,F10.6)", counter, x_lama, temp_galat
					write(12,104) counter, x_lama, temp_galat
				end if
				counter = counter + 1
				if (temp_galat.LT.galat) then
					exit
				end if
				if (counter.GE.batas) then
					print*,"Batas iterasi maksimum : 1000"
					exit
				end if
			end do
			if (ga_ada_solusi) then
				x = 0
			else
				print "(35('-'))"
			end if
		else
			print*, "Fungsi tidak konvergen"
			write(12,*) "Fungsi tidak konvergen"
		end if
	end if
	close(12)
END

subroutine lelaran_jacobi(fungsi_x, fungsi_y, eps, x, y)
	implicit none
	DOUBLE PRECISION fungsi_x, fungsi_y, eps, x, y, x_lama, y_lama
	DOUBLE PRECISION temp_galat_x, temp_galat_y
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(20(' '),A)
102 format(55('-'))
103 format(3X,A,6X,A,9X,A,5X,A,4X,A)
104 format(I4,F15.6,F15.6,F15.6,F15.6)

	print "(20(' '),A)", "Metode Lelaran Jacobi"
	write(12,101) "Metode Lelaran Jacobi"
	print "(55('-'))"
	write(12,102)
	print "(3X,A,6X,A,9X,A,5X,A,4X,A)","r","x","y","Galat x","Galat y"
	write(12,103) "r","x","y","Galat x","Galat y"
	print "(55('-'))"
	write(12,102)
	x_lama = x
	y_lama = y
	print "(I4,F10.6,F10.6,F10.6,F10.6)",counter,x,y,abs(x-x_lama),abs(y-y_lama)
	write(12,104) counter,x,y,abs(x-x_lama),abs(y-y_lama)
	do
		x_lama = x
		y_lama = y
		x = fungsi_x(x_lama,y_lama)
		y = fungsi_y(x_lama,y_lama)
		counter = counter + 1
		temp_galat_x = abs((x-x_lama)/x)
		temp_galat_y = abs((y-y_lama)/y)
print "(I4,F10.6,F10.6,F10.6,F10.6)",counter,x,y,temp_galat_x,temp_galat_y
write(12,104) counter,x,y,temp_galat_x,temp_galat_y
		if ((temp_galat_x.LT.eps).AND.(temp_galat_y.LT.eps)) then
			exit
		end if
		if (counter.GE.batas) then
			print*,"Batas iterasi maksimum : 1000"
			exit
		end if
	end do
	print "(55('-'))"
	write(12,103)
	close(12)
end

subroutine nrp(f_x_proc,f_y_proc,eps, x, y)
	!f_x_proc, f_y_proc prosedur
	implicit none
	DOUBLE PRECISION eps, x, y, x_lama, y_lama
	DOUBLE PRECISION f_x, f_y, f_x_dx, f_x_dy, f_y_dx, f_y_dy, det
	DOUBLE PRECISION temp_galat_x, temp_galat_y
	external f_x_proc, f_y_proc
	integer*2 counter, batas
	parameter(batas=1000)
	counter = 0
	
	open(12,file="out.tmp", action="write", status="replace")
101 format(15(' '),A)
102 format(55('-'))
103 format(3X,A,6X,A,9X,A,5X,A,4X,A)
104 format(I4,F15.6,F15.6,F15.6,F15.6)
	
	print "(15(' '),A)", "Metode Newton Raphson untuk Persamaan"
	write(12,101)"Metode Newton Raphson untuk Persamaan"
	print "(55('-'))"
	write(12,102)
	print "(3X,A,6X,A,9X,A,6X,A,4X,A)","r","x","y","Galat x","Galat y"
	write(12,103)"r","x","y","Galat x","Galat y"
	print "(55('-'))"
	write(12,102)
	x_lama = x
	y_lama = y
	print "(I4,F15.6,F15.6,F15.6,F15.6)",counter,x,y,abs(x-x_lama),abs(y-y_lama)
	write(12,104) counter,x,y,abs(x-x_lama),abs(y-y_lama)
	do
		x_lama = x
		y_lama = y
		call f_x_proc(x, y, f_x, f_x_dx, f_x_dy)	! hitung masing2 nilai fungsi di x, y dan turunan parsial di x,y
		call f_y_proc(x, y, f_y, f_y_dx, f_y_dy)
		det = (f_x_dx * f_y_dy) - (f_x_dy * f_y_dx) ! hitung determinan
		x = x_lama - (((f_x * f_y_dy) + (f_y * f_x_dy)) / det)
		y = y_lama + (((f_x * f_y_dx) - (f_y * f_x_dx)) / det)
		counter = counter + 1
		temp_galat_x = abs((x-x_lama)/x)
		temp_galat_y = abs((y-y_lama)/y)
print "(I4,F15.6,F15.6,F15.6,F15.6)",counter,x,y,temp_galat_x,temp_galat_y
write(12,104)counter,x,y,temp_galat_x,temp_galat_y
		if ((temp_galat_x.LT.eps).AND.(temp_galat_y.LT.eps)) then
			exit
		end if
		if (counter.GE.batas) then
			print*,"Batas iterasi maksimum : 1000"
			exit
		end if
	end do
	print "(55('-'))"
	write(12,102)
	close(12)
end


